﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using HCM_maps_website.Models;

namespace HCM_maps_website.Process.DAL
{
    public class GraphNodeService :NodeService
    {
        
        public List<GraphNodeInfo> GetListNodeRelate(GraphNodeInfo graphNodeStart)
        {
            using (SqlConnection sqlConnect = new SqlConnection(""))
            {
                SqlCommand command = new SqlCommand();
                command.CommandText = "sp_StreetSegment_GetSegmentRelation";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = sqlConnect;
                //command.CommandTimeout = GlobalConfig.TimeOut;
                command.Parameters.AddWithValue("@point", graphNodeStart.Id);
                try
                {
                    sqlConnect.Open();
                }
                catch
                {
                    throw new Exception("Can not connect to database. Please check connect");
                }
                SqlDataReader reader = command.ExecuteReader();
                List<GraphNodeInfo> result = new List<GraphNodeInfo>(); 
                while (reader.Read())
                {
                    GraphNodeInfo graphNodeRelate = new GraphNodeInfo();
                    graphNodeRelate.Id = Convert.ToInt32(reader["ID"]);
                    graphNodeRelate.Location = new Coordinate(Convert.ToDouble(reader["xLocation"]), Convert.ToDouble(reader["yLocation"]));
                    //graphNodeRelate.Length = Distant(graphNodeStart, graphNodeRelate);
                    result.Add(graphNodeRelate);
                }
                result.Sort();
                return result;

            }
            
        }

        public GraphNodeInfo GetGraphNodeInfo(int ID)
        {
            NodeInfo_old nodeInfo = GetNodeInfo(ID);
            GraphNodeInfo result = new GraphNodeInfo();
            result.Id = ID;
            result.Location = nodeInfo.Location;
            return result;

        }
    }
}
